import 'package:enjoy_life/pages/home/components/home_list.dart';
import 'package:enjoy_life/pages/home/components/home_nav.dart';
import 'package:enjoy_life/utils/CheckAuth.dart';
import 'package:enjoy_life/utils/NetworkService.dart';
import 'package:enjoy_life/utils/Toast.dart';
import 'package:flutter/material.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  // 社区公告列表
  List notifyList = [];

  @override
  initState() {
    super.initState();

    // 获取社区公告列表
    getNotifyList();

    Future.delayed(Duration.zero, () async {
      await CheckAuth().init();
      // print(CheckAuth().getToken());
    });
  }

  // 获取社区公告列表
  getNotifyList() async {
    try {
      final res = await http.get('/announcement');
      if (res['code'] != 10000) return CustomToast.error('获取社区公告列表失败');
      // 提示用户
      CustomToast.success('获取数据成功');
      // 更新 社区公告列表 notifyList
      setState(() {
        notifyList = res['data'];
      });
    } catch (e) {
      // print(e);
      CustomToast.error('网络请求错误');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          backgroundColor: Color.fromARGB(255, 210, 221, 232),
          centerTitle: true,
          title: const Text(
            '享+本地生活',
            style: TextStyle(fontSize: 18),
          ),
        ),
        body: Container(
          color: Color.fromARGB(255, 210, 221, 232),
          child: ListView(
            padding: EdgeInsets.all(10),
            children: [
              // Text(notifyList.toString()),
              // 顶部导航栏
              HomeNav(),
              // 中间广告图
              ClipRRect(
                borderRadius: BorderRadius.circular(10.0),
                child: Image.asset('assets/images/banner@2x.jpg'),
              ),
              HomeList(notifyList: notifyList)
            ],
          ),
        ));
  }
}
